﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class NSX_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var list = db.NSX_Get_List();

            var typeorder = list.OrderBy(w => w.HO_TEN);

            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_NSX";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            NSX_Get_ListResult temp = new NSX_Get_ListResult();

            temp.MA_NSX = "...";
            temp.HO_TEN = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.NSX_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "NSX0001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "NSX";
                    for (int j = 0; j < (4 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.NSX_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_NSX.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(TextBox txtMa_NSX, TextBox txtHo_Ten, TextBox txtGhi_Chu)
        {
            if (txtHo_Ten.Text.Trim() == "")
                MessageBox.Show("Chưa nhập tên nhà sản xuất!");
            else
            {
                db.NSX_Insert(txtMa_NSX.Text, txtHo_Ten.Text, txtGhi_Chu.Text);
                MessageBox.Show("Đã thêm thành công");
            }
        }

    }
}
